package de.nordakademie.wpk.boerse.ui.business;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.osgi.framework.ServiceReference;

import de.nordakademie.wpk.boerse.core.data.Depot;
import de.nordakademie.wpk.boerse.core.data.Index;
import de.nordakademie.wpk.boerse.core.data.Stock;
import de.nordakademie.wpk.boerse.core.service.IBoerseService;
import de.nordakademie.wpk.boerse.ui.Activator;
import de.nordakademie.wpk.boerse.ui.job.RetriveDepotJob;
import de.nordakademie.wpk.boerse.ui.job.RetriveIndexJob;
import de.nordakademie.wpk.boerse.ui.job.RetriveStocksJob;

public class BoerseServiceManager {

	private BoerseServiceManager() {
	}

	public static BoerseServiceManager getInstance() {
		return instance;
	}

	public List<Stock> getStocks() {
		// nur alle 2 Minuten neue Daten holen!
		if (((System.currentTimeMillis() - lastUpdateStocks) / 1000) > 120) {
			Job stockJob = new RetriveStocksJob(
					"Ermittlung der aktuellen Aktiendaten!");
			stockJob.addJobChangeListener(new RefreshUIListener());
			stockJob.schedule();
			lastUpdateStocks = System.currentTimeMillis();
		}
		return stocks;
	}

	public Set<Depot> getDepots() {
		// nur alle 2 Minuten neue Daten holen!
		if (dataChanged
				|| ((System.currentTimeMillis() - lastUpdateDepots) / 1000) > 120) {
			Job depotJob = new RetriveDepotJob(
					"Ermittlung der aktuellen Depotdaten!");
			depotJob.addJobChangeListener(new RefreshUIListener());
			depotJob.schedule();
			dataChanged = false;
			lastUpdateDepots = System.currentTimeMillis();
		}
		return depots;
	}

	public List<Index> getIndizes() {
		// nur alle 2 Minuten neue Daten holen!
		if (((System.currentTimeMillis() - lastUpdateIndizes) / 1000) > 120) {
			Job indexJob = new RetriveIndexJob(
					"Ermittlung der aktuellen Indexdaten!");
			indexJob.addJobChangeListener(new RefreshUIListener());
			indexJob.schedule();
			lastUpdateIndizes = System.currentTimeMillis();
		}
		return indizes;
	}

	public void addDepot(Depot depot) {
		getBoerseService().addDepot(depot);
		dataChanged = true;
	}

	public void deleteDepot(Depot depot) {
		getBoerseService().deleteDepot(depot);
		dataChanged = true;
	}

	public boolean isDepotExisting(String name) {
		return getBoerseService().isDepotExisting(name);
	}

	/**
	 * liefert eine Referenz auf den BoerseService
	 * 
	 * @return IBoerseService
	 */
	public IBoerseService getBoerseService() {
		if (service == null) {
			ServiceReference<IBoerseService> serviceReferenz = Activator
					.getDefault().getBundle().getBundleContext()
					.getServiceReference(IBoerseService.class);
			if (serviceReferenz != null)
				service = Activator.getDefault().getBundle().getBundleContext()
						.getService(serviceReferenz);
			else {
				MessageDialog
						.openError(new Shell(), "Verbindungsfehler",
								"Es konnte keine Verbindung zum Server hingestellt werden");
			}
		}
		return service;
	}

	public void setStocks(List<Stock> stocks) {
		this.stocks = stocks;
	}

	public void setDepots(Set<Depot> depots) {
		this.depots = depots;
	}

	public void setIndizes(List<Index> indizes) {
		this.indizes = indizes;
	}

	private IBoerseService service;

	private static BoerseServiceManager instance = new BoerseServiceManager();

	private List<Stock> stocks = new ArrayList<Stock>();

	private Set<Depot> depots = new HashSet<Depot>();

	private List<Index> indizes = new ArrayList<Index>();

	private boolean dataChanged = false;

	private long lastUpdateStocks = 0;

	private long lastUpdateDepots = 0;

	private long lastUpdateIndizes = 0;

	/**
	 * @author Marcus Berndt
	 * 
	 */
	private final class RefreshUIListener extends JobChangeAdapter {
		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse
		 * .core.runtime.jobs.IJobChangeEvent)
		 */
		@Override
		public void done(IJobChangeEvent event) {
			IHandlerService handlerService = (IHandlerService) PlatformUI
					.getWorkbench().getService(IHandlerService.class);
			try {
				handlerService.executeCommand(
						"de.nordakademie.wpk.boerse.ui.refreshUI", null);
			} catch (Exception ex) {
				throw new RuntimeException(
						"de.nordakademie.wpk.boerse.ui.refreshUI Command not found",
						ex);
			}
		}
	}

}
